package com.yanqu.road.dao.impl.activity.greattrade;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.greattrade.config.*;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class GreatTradeConfigDaoImpl extends TempDao {

    private static String getGreatTradeEventInfoSql = "select * from `t_s_activity_bigshop_event` where `activityId`=? ORDER BY `id` ASC";
    public List<GreatTradeEventInfo> getGreatTradeEventInfoList(int activityId) {
        List<GreatTradeEventInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getGreatTradeEventInfoSql);
            int i = 1;
            call.setInt(i, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                GreatTradeEventInfo data = new GreatTradeEventInfo();
                data.setActivityId(rs.getInt("activityId"));
                data.setId(rs.getInt("id"));
                data.setType(rs.getInt("type"));
                data.setTitle(rs.getString("title"));
                data.setReward(rs.getString("reward"));
                data.setExParam(rs.getString("exParam"));
                data.setEndText(rs.getString("endText"));
                list.add(data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    private static String getGreatTradeGoodsInfoSql = "select * from `t_s_activity_bigshop_goods` where `activityId`=? ORDER BY `id` ASC";
    public List<GreatTradeGoodsInfo> getGreatTradeGoodsInfoList(int activityId) {
        List<GreatTradeGoodsInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getGreatTradeGoodsInfoSql);
            int i = 1;
            call.setInt(i, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                GreatTradeGoodsInfo data = new GreatTradeGoodsInfo();
                data.setActivityId(rs.getInt("activityId"));
                data.setId(rs.getInt("id"));
                data.setType(rs.getInt("type"));
                data.setBasePrice(rs.getString("basePrice"));
                data.setChangeWeight(rs.getString("changeWeight"));
                data.setChangeParam(rs.getString("changeParam"));
                data.setExParam(rs.getString("exParam"));
                data.setIcon(rs.getString("icon"));
                data.setQuality(rs.getInt("quality"));
                list.add(data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    private static String getGreatTradeSkillsInfoSql = "select * from `t_s_activity_bigshop_skills` where `activityId`=? ORDER BY `id` ASC";
    public List<GreatTradeSkillsInfo> getGreatTradeSkillsInfoList(int activityId) {
        List<GreatTradeSkillsInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getGreatTradeSkillsInfoSql);
            int i = 1;
            call.setInt(i, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                GreatTradeSkillsInfo data = new GreatTradeSkillsInfo();
                data.setActivityId(rs.getInt("activityId"));
                data.setId(rs.getInt("id"));
                data.setLevel(rs.getInt("level"));
                data.setCost(rs.getInt("cost"));
                data.setSkillParam(StringUtils.stringToIntegerList(rs.getString("skillParam"), "\\|"));
                data.setBeauty(rs.getInt("beauty"));
                list.add(data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    private static String getGreatTradeTitleInfoSql = "select * from `t_s_activity_bigshop_titile` where `activityId`=? ORDER BY `id` ASC";
    public List<GreatTradeTitleInfo> getGreatTradeTitleInfoList(int activityId) {
        List<GreatTradeTitleInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getGreatTradeTitleInfoSql);
            int i = 1;
            call.setInt(i, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                GreatTradeTitleInfo data = new GreatTradeTitleInfo();
                data.setActivityId(rs.getInt("activityId"));
                data.setId(rs.getInt("id"));
                data.setUpgradeLimit(new BigInteger(rs.getString("upgradeLimit")));
                data.setUnlock(rs.getInt("unlock"));
                data.setReward(rs.getString("reward"));
                data.setEndReward(rs.getString("endReward"));
                list.add(data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    private static String getGreatTradeUnlockSkillInfoSql = "select * from `t_s_activity_bigshop_unlock` where `activityId`=? ORDER BY `id` ASC";
    public List<GreatTradeUnlockSkillInfo> getGreatTradeUnlockSkillInfoList(int activityId) {
        List<GreatTradeUnlockSkillInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getGreatTradeUnlockSkillInfoSql);
            int i = 1;
            call.setInt(i, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                GreatTradeUnlockSkillInfo data = new GreatTradeUnlockSkillInfo();
                data.setActivityId(rs.getInt("activityId"));
                data.setId(rs.getInt("id"));
                data.setTitle(rs.getString("title"));
                data.setDesc(rs.getString("desc"));
                data.setParams(StringUtils.stringToStringList(rs.getString("params"), "\\|"));
                list.add(data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }
}
